package view.rules;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import view.grammardevelopment.ViewSemanticsPanel;
import view.rules.TriggerPanel;
import net.miginfocom.swing.MigLayout;

public class StructAdjRulePanel extends JPanel{
	
	private JPanel structPanel;
	private JSplitPane workPanel;
	ViewSemanticsPanel inputPanel;
	SubmitPanel submitPanel;
	TriggerPanel triggerPanel;
	private JPanel componentPanel;
	private JButton insertWordButton;
	private JButton addWordButton;
	private JButton addWordsButton;
	private JButton featuresButton;
	private JButton moveButton;
	private JButton deleteButton;
	private JLabel structLabel;
	private JSpinner structSpin;
	private JDialog verseDialog;
	private JPanel versePanel;
	private JButton currentVerseButton;
	private JLabel bookLabel;
	private JComboBox bookCombo;
	private JLabel chapterLabel;
	private JSpinner chapterSpin;
	private JLabel startVerseLabel;
	private JSpinner startVerseSpin;
	private JLabel endVerseLabel;
	private JSpinner endVerseSpin;
	private JCheckBox ontologyChekBox;
	private JLabel ontologyLabel;
	private JButton okButton;
	private JButton cancelButton;
	private okButtonActionListener verseListener;
	private JPanel ontologyPanel;
	
	private JScrollPane outputPanel;
	
	public StructAdjRulePanel(){
		this.setLayout(new MigLayout());
		initComponents();	
		addComponents();
	}
	
	private void initComponents(){
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		structPanel = new JPanel();
		structPanel.setLayout(new MigLayout());
		structPanel.setPreferredSize(new Dimension((int)screenSize.getWidth() - 50, (int)screenSize.getHeight() - 600));
			
		triggerPanel = new TriggerPanel();
		
		componentPanel = new JPanel();
		componentPanel.setLayout(new MigLayout());
		insertWordButton = new JButton("Insert Word");
		addWordButton = new JButton("Add Word");
		addWordsButton = new JButton("Add Words");
		featuresButton = new  JButton("Features");
		moveButton = new JButton("Move");
		deleteButton = new JButton("Delete");
		structLabel = new JLabel("Structure:");
		SpinnerModel sm = new SpinnerNumberModel(0, 0, 100, 1);
		structSpin = new JSpinner(sm);
		structSpin.setMinimumSize(new Dimension(100, 20));
		componentPanel.add(insertWordButton);
		componentPanel.add(addWordButton);
		componentPanel.add(addWordsButton);
		componentPanel.add(structLabel, "gapleft 50");
		componentPanel.add(structSpin, "wrap");
		componentPanel.add(featuresButton);
		componentPanel.add(moveButton);
		componentPanel.add(deleteButton);
		
		outputPanel = new JScrollPane();
		outputPanel.setBorder(BorderFactory.createTitledBorder("Output"));
		outputPanel.setPreferredSize(new Dimension(((int)screenSize.getWidth() - 50) / 2, (int)screenSize.getHeight() - 250));
		
		inputPanel = new ViewSemanticsPanel("", "", "");
		inputPanel.setPreferredSize(new Dimension(((int)screenSize.getWidth() - 50) , (int)screenSize.getHeight()));
				
		workPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inputPanel, outputPanel);
		workPanel.setOneTouchExpandable(true);
		workPanel.setDividerSize(10);
		workPanel.setDividerLocation((((int)screenSize.getWidth() - 50) / 2) + ((int)screenSize.getWidth() - 50) / 3);
		workPanel.setPreferredSize(new Dimension((int)screenSize.getWidth(),(int)screenSize.getHeight()- 110));
		
		submitPanel = new SubmitPanel();
		
		Handler handler = new Handler();
	    triggerPanel.triggerButton.addActionListener(handler);
		
	}
	
	private void addComponents(){
		
		addToStructPanel();
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, structPanel, workPanel);
		splitPane.setOneTouchExpandable(true);
		add(splitPane, "wrap");
		add(submitPanel);
	}
	
	private void addToStructPanel(){
		
		structPanel.add(triggerPanel,"wrap");
		structPanel.add(componentPanel);
		
	}
	
	private class Handler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if (e.getSource() == triggerPanel.triggerButton){
				
				versePanel = new JPanel();
				versePanel.setLayout(new MigLayout());
				currentVerseButton = new JButton("Current Verse");
				bookLabel = new JLabel("Book:");
				bookCombo = new JComboBox();
				bookCombo.setMinimumSize(new Dimension(100, 20));
				chapterLabel = new JLabel("Chpaters:");
				chapterSpin = new JSpinner();
				chapterSpin.setMinimumSize(new Dimension(100, 20));
				startVerseLabel = new JLabel("Start Verse");
				startVerseSpin = new JSpinner();
				startVerseSpin.setMinimumSize(new Dimension(100, 20));
				endVerseLabel = new JLabel("End Verse:");
				endVerseSpin = new JSpinner();
				endVerseSpin.setMinimumSize(new Dimension(100, 20));
				ontologyPanel = new JPanel();
				ontologyChekBox = new JCheckBox();
				ontologyLabel = new JLabel("Entire Ontology");
				ontologyPanel.add(ontologyChekBox);
				ontologyPanel.add(ontologyLabel);
				okButton = new JButton("OK");
				cancelButton = new JButton("Cancel");
			    verseListener = new okButtonActionListener(versePanel);
				
				verseDialog = new JDialog();
				verseDialog.setTitle("Specify a Start or an End Verse");
				verseDialog.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
				verseDialog.setSize(250, 250);
				verseDialog.setModal(true);
				verseDialog.setLocationRelativeTo(null);
				addToVersePanel();
				verseDialog.add(versePanel);
				verseDialog.setResizable(false);
				
				okButton.addActionListener(verseListener);
				okButton.setActionCommand("OK");
				cancelButton.addActionListener(verseListener);
				cancelButton.setActionCommand("Cancel");
				
				verseListener.setDialogToClose(verseDialog);
				

				verseDialog.setVisible(true);
			}
		}
		
	}
	
	private void addToVersePanel(){
		versePanel.add(currentVerseButton, "wrap");
		versePanel.add(bookLabel);
		versePanel.add(bookCombo, "wrap");
		versePanel.add(chapterLabel);
		versePanel.add(chapterSpin, "wrap");
		versePanel.add(startVerseLabel);
		versePanel.add(startVerseSpin, "wrap");
		versePanel.add(endVerseLabel);
		versePanel.add(endVerseSpin, "wrap");
		versePanel.add(ontologyPanel, "wrap");
		versePanel.add(okButton);
		versePanel.add(cancelButton);
	}
	
	private class okButtonActionListener implements ActionListener {
        private JPanel source;
        private JDialog dialog;
        
        public okButtonActionListener(JPanel source) {
            this.source = source;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        	
            if (e.getActionCommand().equals("OK")) {
                closeDialog();
            } else if (e.getActionCommand().equals("Cancel")) {
                closeDialog();
            }
            
        }
        
        private void closeDialog() {
            if (dialog != null) {
                dialog.dispose();
            }
        }
        
        public void setDialogToClose(JDialog dialog) {
            this.dialog = dialog;
        }
    
    }
}
